﻿using DbUtility;
using STMS.Models.DModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STMS.DAL
{
    public class StoreDAL:BaseDAL<StoreInfo>
    {
        /// <summary>
        /// 关键词查询仓库列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public List<StoreInfo> GetStoreList(string keywords,int isDeleted)
        {
            string strWhere = $"1=1 and IsDeleted={isDeleted}";
            if(!string.IsNullOrEmpty(keywords))
            {
                strWhere += " and (StoreName like @keywords or StoreNo like @keywords)";
            }
            SqlParameter para = new SqlParameter("@keywords", $"%{keywords}%");
            return GetModelList(strWhere, "StoreId,StoreName,StoreNo,RegionCount,Remark", para);
        }

        /// <summary>
        /// 获取所有仓库列表  绑定下拉框
        /// </summary>
        /// <returns></returns>
        public List<StoreInfo> GetAllStores()
        {
            return GetModelList("StoreId,StoreName");
        }

        /// <summary>
        /// 判断仓库是否已存在
        /// </summary>
        /// <param name="storeName"></param>
        /// <returns></returns>
        public bool ExistsStore(string storeName)
        {
            return ExistsByName("StoreName", storeName);
        }

        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="store"></param>
        /// <param name="isGetId"></param>
        /// <returns></returns>
        public int AddStore(StoreInfo store,int isGetId)
        {
            return Add(store, "StoreName,StoreNo,Remark",isGetId);
        }

        /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="store"></param>
        /// <returns></returns>
        public bool UpdateStore(StoreInfo store)
        {
            return Update(store, "StoreId,StoreName,StoreNo,Remark", "");
        }

       //删除直接调用基类的方法

        /// <summary>
        /// 检查是否添加了仓库分区或存储了商品
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns>0 空库，没有分区  1 添加了分区   2 其分区也存储了商品</returns>
        public DataTable  IsAddRegion(int storeId)
        {
            string sql1 = $"select count(1) from StoreRegionInfos where StoreId={storeId}";
            //string sql2 = $"select count(1) from ProductStoreInfos where StoreId={storeId}";
            DataTable  dt = null;
            dt = SqlHelper.GetDataTable(sql1, 1);
            return dt;
        }

        /// <summary>
        /// 获取指定的仓库信息
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public StoreInfo GetStore(int storeId)
        {
            return GetById(storeId, "StoreId,StoreName,StoreNo,Remark,RegionCount");
        }
    }
}
